package com.amkai.aes.webservice.binary.objecthandler.inventory;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;

import com.amkai.aes.aescommon.service.binary.job.inventory.DepratmentFacilitiesJob;
import com.amkai.aes.generatedbeans.organization.Facility;
import com.amkai.aes.generatedbeans.tree.NodeKind;
import com.amkai.aes.webservice.binary.JobObjectHandler;
import com.amkai.common.extensions.hibernate.config.SiteManager;
import com.amkai.common.extensions.hibernate.config.manager.SessionManager;

public class DepartmentFacilitiesHandler implements JobObjectHandler<DepratmentFacilitiesJob,Serializable, ArrayList<Facility> > {
	private static final String query = "from BaseOrganization bo where bo.parent.id = :id and bo.kindWrapper = :kind";
	@Override
	public ArrayList<Facility> process(DepratmentFacilitiesJob job, Serializable object)
			throws Exception {
		
		SessionManager sm = SiteManager.Start();
		try {
			List<Facility> result = new ArrayList<Facility>();
 			sm.openSession();
 			Query q;
 			q = sm.execute(query);
 			q = q.setLong("id", job.getModuleDepartment().getId());
 			q = q.setLong("kind", NodeKind.FACILITY.ordinal());
 			result  = q.list();
 			for (Facility fc : result){
 				fc.getName();
 			}
 			return (ArrayList)result;
		} catch (Exception e) {
			// logger.error("ImportCheckoutHandler: ", e);

			throw e;
		} finally {
			sm.closeSession();
		}
	}

	

}
